package com.fourhats.services.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.fourhats.dao.UserDAO;
import com.fourhats.dao.VisitEntityDAO;
import com.fourhats.model.ObjectTypes;
import com.fourhats.model.User;
import com.fourhats.model.VisitEntity;
import com.fourhats.services.VisitEntityService;

public class VisitEntityServiceImpl implements VisitEntityService {

	@Autowired
	private VisitEntityDAO visitEntityDAO;
	
	@Autowired
	private UserDAO userDAO;

	public void addVisitEntity(int entityType, long entityId, long userId) {
		if(userId != 0 && (entityType == ObjectTypes.PRODUCT.getValue() || entityType == ObjectTypes.MARKET.getValue()) 
				&& this.visitEntityDAO.hasVisitInHour(entityId, entityType, userId, 1)) {
			//Do nothing. Para que un usuario logueado no sume visitas muchas veces si lo visita en una hora
		} else {
			User user = userDAO.getById(userId);
			VisitEntity visitEntity = new VisitEntity(user, entityId, entityType);
			this.visitEntityDAO.add(visitEntity);
		}
	}

	public long getQtyVisits(long entityId, int entityType) {
		return this.visitEntityDAO.getQtyVisits(entityId, entityType);
	}

}
